package com.example.demo.module.develop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.develop.DevelopBomMaterielPoJo;
import com.example.demo.entity.request.develop.DevelopBomMaterielRequest;
import com.example.demo.entity.response.develop.DevelopBomMaterielResponse;
import com.example.demo.module.develop.service.DevelopBomMaterielService;
import com.example.demo.utils.PoJoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author lzy
 * @date 2020-09-09 17:28:01
 */
@RestController
@RequestMapping("/develop/bommateriel")
@Api(tags = "研发BOM物料表接口")
public class DevelopBomMaterielController {
    @Resource
    private DevelopBomMaterielService service;

    /**
     * 分页查询
     */
    @GetMapping
    @ApiOperation(value = "分页查询研发物料表", notes = "根据条件返回集合对象(默认page=1,size=10)")
    public ResponseEntity<Rs<List<DevelopBomMaterielResponse>>> page(DevelopBomMaterielRequest request,
                                                                     @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        request.setCompanyId(companyId);
        Page<DevelopBomMaterielPoJo> result = service.findPage(request);
        List<DevelopBomMaterielResponse> list = PoJoUtil.PageUtil(result, DevelopBomMaterielResponse.class);
        return Rs.ok(Rs.json().put("DevelopBomMaterielResponse", list));
    }

    /**
     * 查询
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询研发物料表", notes = "根据条件返回集合对象")
    public ResponseEntity<Rs<List<DevelopBomMaterielResponse>>> list(DevelopBomMaterielRequest request,
                                                                     @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomMaterielPoJo poJo = new DevelopBomMaterielPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        List<DevelopBomMaterielPoJo> result = service.findList(poJo);
        return Rs.ok(Rs.json().put("listDevelopBomMaterielResponse", PoJoUtil.transToPoJo(result, DevelopBomMaterielResponse.class)));
    }

    /**
     * 批量添加
     */
    @PostMapping("/batch")
    @ApiOperation(value = "批量添加物料表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchadd(@ApiParam(name = "List<PurchaseBomMaterielRequest>", value = "多个物料表批量添加，需要json数组") @RequestBody List<DevelopBomMaterielRequest> request,
                                               @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        service.createBatch(PoJoUtil.transToPoJo(request, DevelopBomMaterielPoJo.class),companyId);
        return Rs.ok();
    }

    /**
     * 单个添加
     */
    @PostMapping
    @ApiOperation(value = "单个添加物料表", notes = "返回msg")
    public ResponseEntity<Rs<Long>> add(@ApiParam(name = "PurchaseBomMaterielRequest", value = "单个物料表添加并且返回Id") @RequestBody DevelopBomMaterielRequest request,
                                        @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomMaterielPoJo poJo = new DevelopBomMaterielPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        return Rs.ok(Rs.json().put("id", service.create(poJo).getId()));
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改物料表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> update(@ApiParam(name = "PurchaseBomMaterielRequest", value = "根据id更新物料表") @RequestBody DevelopBomMaterielRequest request,
                                             @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        DevelopBomMaterielPoJo poJo = new DevelopBomMaterielPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        service.updateById(poJo);
        return Rs.ok();
    }

    /**
     * 彻底批量删除
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除物料表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchdelete(@ApiParam(name = "ids", value = "根据ids彻底删除研发物料表，需要json格式ids数组", required = true) @PathVariable("ids") List<Long> ids) {
        service.batchRemove(ids);
        return Rs.ok();
    }


}
